package com.example.ktdemo.atc

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.example.any_testdemo.sqliteDemo.DbTestActivity
import com.example.any_testdemo.sqliteDemo.TestSqliteOpenHelper
import com.example.kotlin_api.test.Test
import com.example.ktdemo.R
import com.example.ktdemo.databinding.ActStartBinding
import com.example.ktdemo.httputils.HttpUtils
import com.example.ktdemo.httputils.UserApi
import com.example.ktdemo.lis.ResponseListener
import com.example.ktdemo.mvp.model.DragonModel
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import okhttp3.ResponseBody


class StartActivity : AppCompatActivity() {

    private var binding: ActStartBinding? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActStartBinding.inflate(layoutInflater);
        setContentView(binding!!.root)
        binding!!.btnClickCat.setOnClickListener { onClick(it) }
        binding!!.ivStart.setOnClickListener { onClick(it) }
        binding!!.btnClickTackDragon.setOnClickListener { onClick(it) }
        binding!!.btnClickTestAny.setOnClickListener { onClick(it) }
    }

    private fun onClick(v: View?) {
        if (v != null) {
            when (v.id) {
                R.id.btn_click_cat -> clickBtn()
                R.id.iv_start -> clickStart()
                R.id.btn_click_tackDragon -> searchDragon()
                R.id.btn_click_test_any -> anyTest()
                else -> "unUseless"
            }
        }
    }

    //测试AnyTest Lib
    private fun anyTest() {
        val intent = Intent()
        intent.setClass(this,DbTestActivity::class.java)
        startActivity(intent)
    }

    fun searchDragon(){
        XXPermissions.with(this)
            .permission(Permission.MANAGE_EXTERNAL_STORAGE)
            .request(object:OnPermissionCallback{
                override fun onGranted(permissions: MutableList<String>?, all: Boolean) {
                    if (all){
                        Toast.makeText(this@StartActivity,"获取权限成功",Toast.LENGTH_LONG).show()
                    }else{
                        Toast.makeText(this@StartActivity,"获取权限失败",Toast.LENGTH_LONG).show()
                    }
                }

                override fun onDenied(permissions: MutableList<String>?, never: Boolean) {
                    super.onDenied(permissions, never)
                }
            })
     /*   HttpUtils.sendHttp(HttpUtils.createApi(UserApi::class.java).getIndexDragon(),
            object : ResponseListener<DragonModel> {
                override fun onSuccess(t: DragonModel) {
                    println("onSuccess=${t}")
                }

                override fun onFail(msg: String) {
                    println("onFail=$msg")
                }

            })*/
    }

    private fun takeDragon() {
        HttpUtils.sendHttp(HttpUtils.createApi(UserApi::class.java).takeDragon("714093"),
            object : ResponseListener<ResponseBody> {
                override fun onSuccess(t: ResponseBody) {
                    println("onSuccess=$t")
                }

                override fun onFail(msg: String) {
                    println("onFail=$msg")
                }

            })
    }

    private fun clickBtn() {
        val intent = Intent()
        intent.setClass(this, MvpActivity::class.java);
        startActivity(intent)

    }

    private fun clickStart() {
        Test().main()
    }

}